﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Messaging;
using VolunteerManager.ViewModel;
using Microsoft.Practices.ServiceLocation;

namespace VolunteerManager
{
	/// <summary>
	/// WinCreateVolunteer.xaml 的交互逻辑
	/// </summary>
    public partial class WinCreateVolunteer : Window
    {

        public WinCreateVolunteer()
        {
            this.InitializeComponent();

            Messenger.Default.Register<object>(this, "LoadInfo", ShowUserView);
            Messenger.Default.Register<object>(this, "CloseEditWindow", (o) =>
            {
                this.Close();
            });
            
            //卸载当前(this)对象注册的所有MVVMLight消息
            this.Unloaded += WinCreateVolunteer_Unloaded;

            //InitialTray();
        }

        void WinCreateVolunteer_Unloaded(object sender, RoutedEventArgs e)
        {
            Messenger.Default.Unregister(this);
        }

        public void ShowUserView(object obj)
        {
            ServiceLocator.Current.GetInstance<EditViewModel>().CurrentVoluteer = obj as Model.Voluteer;
        }
        #region 处理各复选框
        
        #endregion


        #region 拖盘
        private NotifyIcon notifyIcon = null;
        bool shouldClose = true;
        private void InitialTray()
        {
            shouldClose = false;

            //设置托盘的各个属性
            notifyIcon = new NotifyIcon();
            notifyIcon.BalloonTipText = "程序正在启动....";
            notifyIcon.Text = "TCL接口程序";
            notifyIcon.Icon = new System.Drawing.Icon("../../res/App.ico");
            notifyIcon.Visible = true;
            notifyIcon.ShowBalloonTip(2000);
            notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick);



            //帮助选项
            MenuItem help = new MenuItem("help");

            //关于选项
            MenuItem about = new MenuItem("打开");

            //退出菜单项
            MenuItem exit = new MenuItem("退出");
            exit.Click += new EventHandler(exit_Click);

            //关联托盘控件
            MenuItem[] childen = new MenuItem[] { about, new MenuItem("-"), exit };
            notifyIcon.ContextMenu = new ContextMenu(childen);

            //窗体状态改变时候触发
            this.StateChanged += new EventHandler(SysTray_StateChanged);
        }

        /// <summary>
        /// 鼠标单击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            //如果鼠标左键单击
            if (e.Button == MouseButtons.Left)
            {
                if (this.Visibility == Visibility.Visible)
                {
                    this.Visibility = Visibility.Hidden;
                }
                else
                {

                    this.WindowState = System.Windows.WindowState.Maximized;
                    this.Visibility = Visibility.Visible;
                    this.Activate();
                }
            }
        }

        /// <summary>
        /// 窗体状态改变时候触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SysTray_StateChanged(object sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Minimized)
            {
                this.Visibility = Visibility.Hidden;
            }
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            if (!shouldClose)
            {
                e.Cancel = true;
                Hide();
            }
        }


        /// <summary>
        /// 退出选项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void exit_Click(object sender, EventArgs e)
        {

            shouldClose = true;
            Close();

        }
        #endregion
    }
}